package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.ApArticleRelationService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.behavior.service.ApUnLikeBehaviorService;
import com.heima.model.behavior.dtos.ApArticleRelationDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @作者 itcast
 * @创建日期 2021/4/22 11:22
 **/
@Service
public class ApArticleRelationServiceImpl implements ApArticleRelationService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    ApLikesBehaviorService apLikesBehaviorService;

    @Autowired
    ApUnLikeBehaviorService apUnLikeBehaviorService;

    @Override
    public Map findApArticleRelation(ApArticleRelationDto dto) {
        Map result = new HashMap();
        result.put("islike",false);
        result.put("isunlike",false);
        // 校验参数
        if (dto.getArticleId() == null || dto.getEntryId() == null) {
            return  result;
        }
        if (dto.getType() != 0 && dto.getType() != 1) {
            return  result;
        }
        // 根据参数查询行为实体
        ApBehaviorEntry apBehaviorEntry;
        if(dto.getType() == 0){
            apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(null,dto.getEntryId());
        }else {
            apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(dto.getEntryId(),null);
        }
        if(apBehaviorEntry == null){
            return  result;
        }
        result.put("entryId",apBehaviorEntry.getId());
        // 根据行为实体 查询是否点赞
        ApLikesBehavior likesBehavior = apLikesBehaviorService.getOne(Wrappers.<ApLikesBehavior>lambdaQuery()
                .eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId())
        );
        if(likesBehavior != null && likesBehavior.getOperation().intValue() == 0){
            result.put("islike",true);
        }
        // 根据行为实体 查询是否不喜欢
        ApUnlikesBehavior unlikesBehavior = apUnLikeBehaviorService.getOne(Wrappers.<ApUnlikesBehavior>lambdaQuery()
                .eq(ApUnlikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApUnlikesBehavior::getEntryId, apBehaviorEntry.getId())
        );
        if(unlikesBehavior != null && unlikesBehavior.getType().intValue() == 0){
            result.put("isunlike",true);
        }
        // 封装Map结果 返回   lslike      lsunlike    entryId
        return result;
    }
}
